<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
use Common\Model\Goods;
use Common\Model\Brand;
use Common\Model\GoodsAttr;
use Common\Model\TypeAttr;
use Common\Model\GoodsDetail;
use Common\Model\Cate;
use Common\Model\Content;
use Common\Model\OrderList;


//内容控制器
class ContentController extends Controller{

	//构造函数
	public function __init()
	{
	}
	
    //首页
    public function index(){
		
    	$goodsModel = new Goods();
		$contentModel = new Content();
		$brandModel = new Brand();
		$goodsDetailModel = new GoodsDetail();
		$orderListModel = new OrderList();
		
		//处理'产品分类'
		
		$secondCate = $contentModel->getGoodsCate();
		View::with('secondCate',$secondCate);
		
		//处理'最热排行'
		$hotData = $goodsModel->orderBy('click','DESC')->limit(10)->get();
		View::with('hotData',$hotData);
		
		//处理‘热销排行’-关联商品表和订单列表,找出购买数量多的商品
		$hotRank = $goodsModel
				->join('order_list',C('database.prefix').'goods.gid','=',C('database.prefix').'order_list.wsshop_goods_gid')
				->orderBy('quantity','DESC')
				->limit(10)
				->GroupBy(C('database.prefix').'goods.gid')
				->get();
		View::with('hotRank',$hotRank);
		
		
		
		//1.显示对应商品信息,需要查询商品表和商品属性表，还有对应的类型属性表
		//查询商品表,获取商品数据并分配到页面
		$gid = Q('get.gid');//post过来的gid
		$goodsData = $goodsModel->where("gid={$gid}")->find();//根据提供的gid查询对应商品信息
		$subject = $goodsData['listview'];//取得列表图的路径
		$view =  pathinfo($goodsData['listview']);//获取列表图的信息
		$search = '_list.'."{$view['extension']}";
		$replace = '.'."{$view['extension']}";
		$listview =  str_ireplace($search, $replace, $subject);//把_list.jpg替换成.jpg
		$goodsData['listview'] = $listview;//替换商品信息中的列表图路径，输出他的原图
//		p($goodsData);
		View::with('goodsData',$goodsData);
		
		//2.查询品牌表，获取对应的品牌信息并分配到页面
		$bid = $goodsData['wsshop_brand_bid'];
		$brandData = $brandModel->where("bid={$bid}")->first();
		View::with('brandData',$brandData);

		//3.根据商品的gid查询商品属性表找到类型属性id，去重复然后查询类型属性表查询class为规格的tavalue
		$goodsAttrModel = new GoodsAttr();
		$typeAttrData = $goodsAttrModel->getTA($gid);
		View::with('typeAttrData',$typeAttrData);
		
		//4.处理产品详情模块-查询商品详情表
		$detail = $goodsDetailModel->getDetail($gid);
		View::with('detail',$detail);
		
       	View::make();
    }

}
